package net.zjitc.controller;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Attribute;
import net.zjitc.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 毛钰深
 * @Create 2021/12/17 10:31
 * 分类参数
 */
@RestController
public class AttributeController {

    @Autowired
    private AttributeService attributeService;

    /**
     * 获取参数列表  参数都不能为空
     * @param catId  传入的分类id   cat_id
     * @param sel 传入的获取动态还是静态参数
     * @return 参数列表
     */
    @PreAuthorize("hasAuthority('142')")
    @GetMapping("categories/{catId}/attributes")
    public ResponseResult<List<Attribute>> findAttributes(@PathVariable Integer catId, @RequestParam String sel) {
        return attributeService.findAttributeByCatIdAndSel(catId, sel);
    }

    /**
     * 添加参数
     * @param id
     * @param attribute
     * @return
     */
    @PreAuthorize("hasAuthority('143')")
    @PostMapping("categories/{id}/attributes")
    public ResponseResult<Attribute> addAttribute(@PathVariable Integer id,@RequestBody Attribute attribute){
        return attributeService.addAttribute(id,attribute);
    }

    /**
     * 删除参数
     * @param id
     * @param attrId
     * @return
     */
    @PreAuthorize("hasAuthority('144')")
    @DeleteMapping("categories/{id}/attributes/{attrId}")
    public ResponseResult<Attribute> deleteAttribute(@PathVariable Integer id,@PathVariable Integer attrId){
        return attributeService.deleteAttribute(id,attrId);
    }

    /**
     * 根据id查询相关数据
     * @param id
     * @param attrId
     * @return
     */
    @PreAuthorize("hasAuthority('142')")
    @GetMapping("categories/{id}/attributes/{attrId}")
    public ResponseResult<Attribute> selectById(@PathVariable Integer id,@PathVariable Integer attrId){
        return attributeService.selectById(id,attrId);
    }

    /**
     * 修改参数
     * @param id
     * @param attrId
     * @param attribute
     * @return
     */
    @PreAuthorize("hasAuthority('160')")
    @PutMapping("categories/{id}/attributes/{attrId}")
    public ResponseResult<Attribute> updateAttribute(@PathVariable Integer id,@PathVariable Integer attrId,@RequestBody Attribute attribute){
        return attributeService.updateAttribute(id,attrId,attribute);
    }

}
